<script lang="ts" setup>
import { ref, reactive, watch } from "vue"
import { formatDateTime } from "@/utils"
import { usePagination } from "@/hooks/usePagination"
import { Search, Refresh } from "@element-plus/icons-vue"
import { type FormInstance } from "element-plus"
import { useTeamStore } from "@/store/modules/team"
import { getRiskOrderApi } from "@/api/RiskControlCenter/index"
import { shortcuts } from "@/utils/dateSet"

defineOptions({
  name: "RiskControlCenterOrder"
})
const { paginationData, handleCurrentChange, handleSizeChange } = usePagination()
// #region 搜索内容
const defaultTime1: [Date, Date] = [new Date(2000, 1, 1, 0, 0, 0), new Date(2000, 2, 1, 23, 59, 0)]
const searchFormRef = ref<FormInstance | null>(null)
const searchData = reactive<any>({
  time: "",
  id: "", //订单来源
  name: "", //联系人姓名
  phone: "", //联系人电话
  card_name: "", //身份证姓名
  card_id: "" //身份证号
})
// 团队持久
const tableDataB = ref<any[]>([])
const teamStore = useTeamStore()
tableDataB.value = teamStore.teamListStore
//
const resetSearch = () => {
  searchFormRef.value?.resetFields()
  if (paginationData.currentPage === 1) {
    getTableData()
    console.log("重置")
  }
  paginationData.currentPage = 1
}
const handleSearch = () => {
  if (paginationData.currentPage === 1) {
    getTableData()
    console.log("查询")
  }
  paginationData.currentPage = 1
}
// #endregion

//  #region 获取订单
const tableData = ref<any>([])
const getTableData = async () => {
  if (!searchData.time) {
    searchData.time = []
  }
  await getRiskOrderApi({
    start: new Date(searchData.time[0]).getTime() || "",
    end: new Date(searchData.time[1]).getTime() || "",
    id: searchData.id || "",
    name: searchData.name,
    phone: searchData.phone,
    card_name: searchData.card_name,
    card_id: searchData.card_id,
    page: paginationData.currentPage,
    size: paginationData.pageSize
  }).then((res: any) => {
    paginationData.total = res.data.total
    tableData.value = res.data.list
    console.log(res, "所有订单")
  })
}

/** 监听分页参数的变化 */
watch([() => paginationData.currentPage, () => paginationData.pageSize], getTableData, { immediate: true })
// #endregion
</script>

<template>
  <div class="app-container">
    <el-card shadow="never" class="search-wrapper">
      <el-form ref="searchFormRef" :inline="true" :model="searchData">
        <el-form-item prop="time" label="日期">
          <el-date-picker
            v-model="searchData.time"
            type="datetimerange"
            range-separator="到"
            start-placeholder="开始时间"
            end-placeholder="结束时间"
            :shortcuts="shortcuts"
            :default-time="defaultTime1"
          />
        </el-form-item>
        <el-form-item label="来源查询" prop="id">
          <el-input v-model.trim="searchData.id" clearable placeholder="请输入" style="width: 240px" />
        </el-form-item>
        <el-form-item label="联系人姓名" prop="name">
          <el-input v-model.trim="searchData.name" clearable placeholder="请输入" style="width: 240px" />
        </el-form-item>
        <el-form-item label="联系人电话" prop="phone">
          <el-input v-model.trim="searchData.phone" clearable placeholder="请输入" style="width: 240px" />
        </el-form-item>
        <el-form-item label="身份证姓名" prop="card_name">
          <el-input v-model.trim="searchData.card_name" clearable placeholder="请输入" style="width: 240px" />
        </el-form-item>
        <el-form-item label="身份证号" prop="card_id">
          <el-input v-model.trim="searchData.card_id" clearable placeholder="请输入" style="width: 240px" />
        </el-form-item>
        <el-form-item>
          <el-button type="primary" :icon="Search" @click="handleSearch">查询</el-button>
          <el-button :icon="Refresh" @click="resetSearch">重置</el-button>
        </el-form-item>
      </el-form>
    </el-card>
    <el-card shadow="never">
      <div class="table-wrapper">
        <el-table :data="tableData">
          <el-table-column type="index" label="序号" width="120" align="center">
            <template #default="{ $index }">
              {{ (paginationData.currentPage - 1) * paginationData.pageSize + $index + 1 }}
            </template>
          </el-table-column>
          <el-table-column prop="risk_time" label="日期" align="center" width="180">
            <template #default="scope">
              {{ formatDateTime(scope.row.date) }}
            </template>
          </el-table-column>
          <el-table-column prop="source" label="订单来源" align="center" />
          <el-table-column prop="name" label="联系人姓名" align="center" />
          <el-table-column prop="phone" label="联系人电话" align="center" />
          <el-table-column prop="address" label="地址" align="center" />
          <el-table-column prop="card_name" label="身份证姓名" align="center" />
          <el-table-column prop="card_id" label="身份证号" align="center" />
          <el-table-column prop="note" label="情况说明" align="center" />
        </el-table>
      </div>
      <div class="pager-wrapper">
        <el-pagination
          background
          :layout="paginationData.layout"
          :page-sizes="paginationData.pageSizes"
          :total="paginationData.total"
          :page-size="paginationData.pageSize"
          :currentPage="paginationData.currentPage"
          @size-change="handleSizeChange"
          @current-change="handleCurrentChange"
        />
      </div>
    </el-card>
  </div>
</template>

<style lang="scss" scoped>
.search-wrapper {
  margin-bottom: 20px;
}
.table-wrapper {
  margin-bottom: 20px;
}
.pager-wrapper {
  display: flex;
  justify-content: flex-end;
}
</style>
